--  查看會員交易紀錄
local httparg    = require("shared.api.httparg")
local response   = require("shared.api.response")
local config     = require("config")
local tag        = httparg.tag()
local assertion  = httparg.assertion
local httpclient = require "shared.http.client"
local def        = require "shared.api.def"

local cjson      = require "cjson.safe"
cjson.encode_max_depth(32)
cjson.decode_max_depth(32)


local ARGS = {
  tag.body('json', 'required'),

  id                    = tag.json.id                     ('string', assertion.non_empty_string()),-- 交易編號
  transaction_provider  = tag.json.transaction_provider   ('string', assertion.non_empty_string()),-- 原始交易來源
  transaction_id        = tag.json.transaction_id         ('string', assertion.non_empty_string()),--原始交易編號
  type                  = tag.json.type                   ('string', assertion.non_empty_string()),
  account               = tag.json.account                ('string', assertion.non_empty_string()),
  username              = tag.json.username               ('string', assertion.non_empty_string()),
  wallet                = tag.json.wallet                 ('string', assertion.non_empty_string()),
  coin                  = tag.json.coin                   ('string', assertion.non_empty_string()),
  create_after          = tag.json.create_after           ('string', assertion.non_empty_string()),
  create_before         = tag.json.create_before          ('string', assertion.non_empty_string()),
  commit_after          = tag.json.commit_after           ('string', assertion.non_empty_string()),
  commit_before         = tag.json.commit_before          ('string', assertion.non_empty_string()),
  count                 = tag.json.count                  ('string', assertion.non_empty_string()),--返回的筆數
  offset                = tag.json.offset                 ('string', assertion.non_empty_string()),-- 頁面偏移量
}

local result={}
do
  local headers               = ngx.req.get_headers()
  local query                 = nil
  local util                  = require "proxy.game.common.util"
  local fill_response_header  = util.fill_response_header
  local is_match_content_type = util.is_match_content_type
  local get_content_decoder   = util.get_content_decoder
  local location              = config.ENV.DATAGATE_SERVICE .. "/transaction"
  local method                = "LIST"

  do
        headers['X-Forwarded-For'] = ngx.var.proxy_add_x_forwarded_for
  end

  local rmvLowKey                  = "x-http-method-override"
  local rmvKey2                    = "accept-encoding"
  for k,v in pairs(headers) do
      local low=string.lower(k)
      if low==rmvLowKey  or low==rmvKey2   then
          headers[k]=nil
      end
  end

  local bodyMap  = {
        id                   = ARGS.id,
        transaction_provider = ARGS.transaction_provider,
        type                 = ARGS.type,
        account              = ARGS.account,
        username             = ARGS.username,
        wallet               = ARGS.wallet,
        coin                 = ARGS.coin,
        create_after         = ARGS.create_after,
        create_before        = ARGS.create_before,
        commit_after         = ARGS.commit_after,
        commit_before        = ARGS.create_before,
        count                = ARGS.count,
        offset               = ARGS.offset
  }

  local   requestData = cjson.encode(bodyMap)
  local   resp, err   = httpclient.new()
                                :uri(location)
                                :headers(headers)
                                :query(query)
                                :body(requestData)
                                :send(method, 2000, nil)

  if err or (not resp) then
      ngx.status = 500
      ngx.say(err)
      ngx.exit(ngx.OK)
  end

  if not resp.body
          or not resp.headers
          or resp.status ~= 200
          or not is_match_content_type(resp, 'application/json')  then

      ngx.status = resp.status
      fill_response_header(resp.headers)
      ngx.say(resp.body)
      ngx.exit(ngx.OK)
  end

  local respBody
  local contentDecoder = get_content_decoder(resp)
  if contentDecoder then
              respBody = contentDecoder.decode(resp.body)
  else
      ngx.status = 500
      ngx.say(err)
      ngx.exit(ngx.OK)
  end

   result=respBody
   for i, v in ipairs(result.content) do
       if v.type=="deposit" then
   result.content[i].status="fulfilled"
       elseif v.type=="withdraw" then
           local peekRequest={
                   id = v.id
           }
          location    = config.ENV.DATAGATE_SERVICE .. "/withdrawal_slip"
          method      = "PEEK"
          requestData = cjson.encode(peekRequest)
          resp, err   = httpclient.new()
                                         :uri(location)
                                         :headers(headers)
                                         :query(query)
                                         :body(requestData)
                                         :send(method, 2000, nil)

           if err or (not resp) then
               ngx.status = 500
               ngx.say(err)
               ngx.exit(ngx.OK)
           end

           if not resp.body
                   or not resp.headers
                   or resp.status ~= 200
                   or not is_match_content_type(resp, 'application/json')  then

               ngx.status = resp.status
               -- fill header except 'Server'
               fill_response_header(resp.headers)
               ngx.say(resp.body)
               ngx.exit(ngx.OK)
           end

           contentDecoder = get_content_decoder(resp)
           if contentDecoder then
                 respBody = contentDecoder.decode(resp.body)
           else
               -- 这儿理论上不应该出错
               ngx.status = 500
               ngx.say(err)
               ngx.exit(ngx.OK)
           end
           result.content[i].status=respBody.status
       end

   end

  response.success(result)

end
